/**
 * 渠道运营－中介机构-未审核
 */
define(function (require, exports, module) {
    var $ = require("jquery");
    require("pagination");
    var util = require('util');
    var ui = require('ui');
    var dialog = require("dialog");
    var angular = require("angular");
    var api_prefix = G_CONFIG.api_prefix + "qmyx/agency/";

    angular.module('Unreview', ['ngUI'])
        .controller('UnReviewController', ['$rootScope', '$scope', '$http',
            function ($rootScope, $scope, $http) {
                function doGetData(params) {
                    $scope.loading = true;
                    $http
                        .get(api_prefix + "get-agency-list", {params: params})
                        .then(function (res) {
                            $scope.loading = false;
                            var retCode = res.data.retCode;
                            if (retCode == 0) {
                                $scope.data = res.data.data;
                                $scope.list = $scope.data.items;
                                $scope.total = $scope.data.total;
                            } else {
                                ui.showErrTip(res.data.errMsg);
                                return false;
                            }
                        })
                        .finally(function () {
                            $scope.loading = false;
                        });
                };

                var getData = function (pageIndex) {
                    pageIndex = pageIndex || 1;
                    var params = {
                        curPage: pageIndex,
                        shenhe: 0,
                        regStart: $scope.filter.startDate,
                        regEnd: $scope.filter.endDate
                    };
                    //params[$scope.search.queryKey] = $scope.search.keyword;

                    doGetData(params);
                };
                $scope.getData = getData;

                //查询未审核中介机构列表
                $scope.getFilterData = function () {
                    $scope.pageIndex = 1;
                    getData();
                };


                //搜索
                $scope.search = {
                    queryKey: "agencyName", keyword: "",
                    fieldList : [
                        {value:'agencyName',text:'机构公司名称'},
                        {value:'personInCharge',text:'负责人姓名'},
                        {value:'mobileTel',text:'负责人电话'}
                    ]
                };
                //搜索获取未审核列表
                var getSearchData = function (pageIndex) {
                    pageIndex = pageIndex || 1;
                    var params = {
                        curPage: pageIndex,
                        shenhe: 0
                    };
                    params[$scope.search.queryKey] = $scope.search.keyword;
                    $scope.pageIndex = 1;
                    doGetData(params);
                };
                $scope.getSearchData = getSearchData;
                $scope.filter = {
                    keyword: '',
                    queryKey: 'agencyName',
                    pageSize: 10,
                    // 撤销过滤
                    clearFilter: function () {
                        $scope.filter.startDate = '';
                        $scope.filter.endDate = '';
                        $scope.getFilterData();
                    }
                }

                //中介机构审核通过
                $scope.passReview = function (item) {
                    util.confirm('确定审核通过吗？', {
                        ok: function () {
                            $http
                                .post(api_prefix + "audit-pass", {token: util.getToken(), agencyId: item.b_agenciesId})
                                .success(function (res) {
                                    var retCode = res.retCode;
                                    if (retCode == 0) {
                                        getSearchData();
                                        ui.showSucTip();
                                    } else {
                                        ui.showErrTip(res.errMsg);
                                    }
                                })
                        }
                    })
                };

                //中介机构审核不通过
                $scope.unpassReview = function (item) {
                    util.confirm('确定审核不通过吗？', {
                        ok: function () {
                            $http
                                .post(api_prefix + "audit-fail", {token: util.getToken(), agencyId: item.b_agenciesId})
                                .success(function (res) {
                                    var retCode = res.retCode;
                                    if (retCode == 0) {
                                        getSearchData();
                                        ui.showSucTip();
                                    } else {
                                        ui.showErrTip(res.errMsg);
                                    }
                                })
                        }
                    })
                };


                //导出未审核机构列表
                $scope.exportData = function (e) {
                    var params = {
                        shenhe: 0,
                        regStart: $scope.filter.startDate,
                        regEnd: $scope.filter.endDate
                    };
                    params[$scope.search.queryKey] = $scope.search.keyword;
                    $(e.target).attr('href', api_prefix + "export&" + $.param(params));
                };

                //获取未审核列表数据
                getData(1);

            }
        ])
    ;
})